package com.mty.mydemo.adapter;

import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.mty.mydemo.R;
import com.mty.mydemo.View.PinnedSectionListView;
import com.mty.mydemo.act.BaseActivity;
import com.qunduikj.sjb.sdk.consts.AppConst;

import java.util.List;
import java.util.Map;

/**
 * @ClassName:${PinnedSectionAdapter}
 * @Description：顶住顶部listview适配器
 * Created by ${mty} on 2016/3/12.
 * http://blog.csdn.net/yunyuliunian
 */
public class PinnedSectionAdapter implements PinnedSectionListView.PinnedSectionListAdapter {


    List<Map<String, Object>> list;
    BaseActivity activity;

    public PinnedSectionAdapter(List<Map<String, Object>> list, BaseActivity activity) {
        this.list = list;
        this.activity = activity;
    }

    //重写次方法 如果是ture则在顶部显示直到下一个这个类
    @Override
    public boolean isItemViewTypePinned(int viewType) {
        return viewType == AppConst.ShowTop;
    }


    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    //是否能接收点击事件
    @Override
    public boolean isEnabled(int position) {
        return getItemViewType(position)==AppConst.Normal;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Map<String, Object> map = (Map<String, Object>) getItem(position);
        ViewHolder viewholder;
        if (convertView==null){
            convertView=activity.getLayoutInflater().inflate(R.layout.item_pinedtest,null);
            viewholder=new ViewHolder();
            viewholder.tv= (TextView) convertView.findViewById(R.id.item_pinned_tv);
            convertView.setTag(viewholder);
        }else {
            viewholder= (ViewHolder) convertView.getTag();
        }
        viewholder.tv.setText(map.get("show").toString());
        if(isItemViewTypePinned(getItemViewType(position))){
            convertView.setBackgroundColor(activity.getResources().getColor(R.color.yellow));
        }else {
            convertView.setBackgroundColor(activity.getResources().getColor(R.color.white));
        }
        return convertView;
    }

    class ViewHolder{
        TextView tv;
    }


    @Override
    public int getItemViewType(int position) {
        return (int)((Map<String, Object>) getItem(position)).get(AppConst.IsShowTop);
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }
}

//        pinnedSectionListView = (PinnedSectionListView) findViewById(R.id.pinnedlv);
//        list = new ArrayList<>();
//        Map<String, Object> map;
//        for (int i = 0; i < 10; i++) {
//        map=new HashMap<>();
//        map.put(AppConst.IsShowTop,AppConst.ShowTop);
//        map.put("show",i+"测试");
//        list.add(map);
//        for (int j = 0; j < 10; j++) {
//        map=new HashMap<>();
//        map.put(AppConst.IsShowTop,AppConst.Normal);
//        map.put("show",j+10*i+"测试");
//        list.add(map);
//        }
//        }
//        pinnedSectionListView.setAdapter(new PinnedSectionAdapter(list,this));